import java.util

import scala.collection.mutable
import scala.collection.JavaConversions._
/**
  * Created by 6v on 2018/5/3.
  */
object ShowDemo {


  def main(args: Array[String]): Unit = {

    val flag = true

    val newArry= (1 to 10).map(_*2)

    // if else 表达式有值
    val a = if (flag) 1 else 0
    // 类型放在后
    val answer, message: String = "yes"
    //scala 不支持 ++ --
    var c = 0
    c += 1
    var y = 0
    //scala中不能多重赋值,
    val x = y = 1
    //如果需要给x赋值 val x = ｛y = 1;2｝
    println(x + "===" + y)
    //for 如果不想包含上边界，可以使用until
    //    for(i <- 1 to 10){
    //      println(10)
    //    }

    //    for(i <- 1 to 10 if i!=5; if i!=6){
    //      println(i)
    //    }

        for(i <- 1 to 3; j <- 1 to i){
          println(i +"========="+j)
        }
    val e = for (i <- 1 to 3; j <- 1 to i)
      yield (i,j)
    println(e)

    //元组
    val myTuple = (1, 3.14, "元组", true)
    val (number, float, string, bool) = myTuple
    //从1开始计数
    println(myTuple._1)
    println(myTuple._2)


    var sum = 0
    List(1, 2, 3, 4, 5).foreach(x => sum += x)
    println("sum"+sum)
    //高阶函数
    //函数参数，即传入另一个函数的参数是函数
    println(convertIntToString((x: Int) => x + " s"))

    val f=multiplyBy(10)
    println(f(50))

    println( apply( layout, 10) )


    import ImplicitHelper.{ImpAdd, echo, strToInt}
    println(1.add(2))
    //import com.donews.localspark.ImplicitHelper.strToInt 源码一般放在上面
    println(strToInt("1"))
    println(math.max("1", 2))

    echo("hello")("word")
    import ImplVal.name
    echo("hello")


    import scala.collection.JavaConverters._
    val list = new util.ArrayList[String]()
    list.add("oooo")
    list.asScala
    val sl = new scala.collection.mutable.ListBuffer[Int]
    sl.asJava
    val orderIds: java.util.List[String] = Seq("SJ001", "SJ002").asJava

    var r:Any =""
    val res  = r match {
      case   1 | "1" | "one"  => "one "
      case "two"=> 2
      case s:String => "String"
      case y:Int=>"Int 类型 "
      case (m,n) => //
      case Some(v) => //
      case _ => "其他"//default
    }
    val arr = Array(1)
    try{
      arr(1)
    }catch{
      case _:ArrayIndexOutOfBoundsException => println("ArrayIndexOutOfBoundsException")
      case ex:Exception => ex.printStackTrace()
    }


    import scala.collection.JavaConversions
//    mutable.
//    immutableMapAdapter
  }

  //省略返回值类型声明，scala会根据=号后边的表达式的类型推断返回值类型
  def abs(x: Double) = if (x >= 0) x else -x

  def fac(n: Int) = {
    var r = 1
    for (i <- 1 to n) r = r * i
    r
  }

  //对于递归函数必须指定返回值类型
  def facRec(n: Int): Int = if (n <= 0) 1 else n * fac(n - 1)


  def convertIntToString(f: (Int) => String) = f(4)

  //返回值是函数的函数
  def multiplyBy(factor: Double) = (x: Double) => factor * x

  def apply(f: Int => String, v: Int) = f(v)

  def layout[A](x: A) = "[" + x.toString() + "]"

  def curriedSum(x: Int)(y: Int) = x + y
  def add(x:Int,y:Int)=x+y

  def person(implicit name : String) = name


}
